package com.mzy.javaapi.cycleproblem;

import org.testng.annotations.Test;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/6/21 14:16
 * @Version 1.0
 */
public class ForCycle {
    public static void main(String[] args) {

    }

    /**
     * break 跳出循环
     */
    @Test
    public void forCycle01() {
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(1);
        objects.add(2);
        objects.add(3);
        for (Integer item :
                objects) {
            if (item.equals(2)) {
                break;
            }
            System.out.println(item);

        }
    }

    /**
     * 跳出本次循环剩下的语句。
     */
    @Test
    public void forCycle02(){
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(1);
        objects.add(2);
        objects.add(3);
        for (Integer item :
                objects) {
            if (item.equals(2)) {
               continue;
            }
            System.out.println(item);

        }
    }

    /**
     * return 跳出当前[方法]循环
     */
    @Test
    public void interrupt(){
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(1);
        objects.add(2);
        objects.add(3);
        objects.add(4);
        for (Integer item :
                objects) {
            if (item.equals(3)) {
               return;
            }
            System.out.println(item);
        }

        System.out.println("是否程序已被截断");
    }
}
